﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel.Syndication;
using B10.Models;
using B10.Helpers;

namespace B10.Controllers
{
    [HandleError]
    public class RssController : Controller
    {
        IDatabaseRepository _db;

        public RssController()
            : this(new DatabaseRepository())
        { }

        public RssController(IDatabaseRepository db)
        {
            _db = db;
        }

        //
        // GET: /Rss/        
        public ActionResult Index(PagesSortOrder? sort)
        {
            const int pageSize = 15;
            var pages = _db.GetActivePages();

            pages = GetOrderedPages(pages, sort).Take(pageSize);
            List<SyndicationItem> items = new List<SyndicationItem>();

            foreach (var p in pages)
            {
                string url = String.Format("http://{0}{1}", Request.Url.Authority,
                    Url.Action("Page", "Home", new { pageUrl = p.Url, id = p.Id }));
                SyndicationItem item = new SyndicationItem();
                item.Title = SyndicationContent.CreatePlaintextContent(p.Title);
                item.Summary = SyndicationContent.CreateHtmlContent(p.Description);
                if (p.PublishDate.HasValue)
                    item.PublishDate = p.PublishDate.Value;
                item.Id = url;
                item.AddPermalink(new Uri(url));

                items.Add(item);
            }           

            string title = GetTitle(sort);

            SyndicationFeed feed = new SyndicationFeed(title, "", null, items);
            
            return new RssResult(feed);
        }

        private string GetTitle(PagesSortOrder? sort)
        {
            if (!sort.HasValue)
                return Request.Url.Authority + " - Strona główna";
            switch (sort.Value)
            {
                case PagesSortOrder.Newest:
                    return Request.Url.Authority + " - Najnowsze";
                case PagesSortOrder.TopAll:
                    return Request.Url.Authority + " - Najpopularniejsze";
                case PagesSortOrder.TopDay:
                    return Request.Url.Authority + " - Top Dnia";
                case PagesSortOrder.TopMonth:
                    return Request.Url.Authority + " - Top miesiąca";
                case PagesSortOrder.TopWeek:
                    return Request.Url.Authority + " - Strona główna";
            }
            return Request.Url.Authority + " - Strona główna";
        }

        private IQueryable<UserPage> GetOrderedPages(IQueryable<UserPage> list,
            PagesSortOrder? sortOrder)
        {
            if (!sortOrder.HasValue)
                return list.OrderByDescending(p => p.PageViews);
            switch (sortOrder)
            {
                case PagesSortOrder.Newest:
                    return list.OrderByDescending(p => p.PublishDate);
                case PagesSortOrder.TopAll:
                    return list.OrderByDescending(p => p.PageViews);
                case PagesSortOrder.TopDay:
                    return list.OrderByDescending(p => p.PageViewsDay);
                case PagesSortOrder.TopMonth:
                    return list.OrderByDescending(p => p.PageViewsMonth);
                case PagesSortOrder.TopWeek:
                    return list.OrderByDescending(p => p.PageViewsWeek);
                default:
                    return list.OrderByDescending(p => p.PageViews);
            }
        }

    }
}
